package com.example.randox;

import java.util.ArrayList;
import java.util.List;

import library.DatabaseHandler;
import library.UserFunctions;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.technics.Utils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends Activity{

	 // JSON Response node names
    private static String KEY_SUCCESS = "success";
    private static String KEY_ERROR = "error";
    private static String KEY_FIRSTNAME = "firstname";
    private static String KEY_LASTNAME = "lastname";
    private static String KEY_EMAIL = "email";
    private static String KEY_PASSWORD = "password";
    private static String KEY_AGE = "age";
    private static String KEY_SEX = "sex";
    		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_register);
		
		final EditText firstname = (EditText)findViewById(R.id.editText1);
		final EditText lastname = (EditText)findViewById(R.id.editText2);
		final EditText email = (EditText)findViewById(R.id.editText3);
		final EditText password = (EditText)findViewById(R.id.editText4);
		final EditText repeat = (EditText)findViewById(R.id.editText5);
		final EditText age = (EditText)findViewById(R.id.editText6);
		final Spinner sex = (Spinner) findViewById(R.id.spinner1);
		
		TextView signup = (TextView)findViewById(R.id.button1);
		
		//TextView returnLogin = (TextView)findViewById(R.id.returnLogin);
		
		 final List<EditText> ListFields = new ArrayList<EditText>(){{
			 add(firstname);
			 add(lastname);
			 add(email);
			 add(password);
			 add(repeat);
			 add(age);
		}};
		 
		signup.setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {
						boolean fieldEmpty=false;
						boolean errors=false;
						boolean checkPassword=false;
			    		 for (EditText field : ListFields) {
			    			    if(TextUtils.isEmpty(field.getText().toString())){
			    			    	field.requestFocus();
					    		 	field.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_error, 0);
					    		 	if(!fieldEmpty==true){
		    			    			Toast.makeText(RegisterActivity.this, "Please complete all fields", Toast.LENGTH_SHORT).show();
		    			    		}
					    		 	fieldEmpty=true;
					    		 	errors=true;	
			    			    }else{
			    			    	
			    			    	if(!TextUtils.isEmpty(password.getText().toString()) || !TextUtils.isEmpty(repeat.getText().toString())){
					    			    	if(!password.getText().toString().equals(repeat.getText().toString())){
					    			    		password.requestFocus();
					    			    		repeat.requestFocus();
					    			    		password.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_error, 0);
					    			    		repeat.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_error, 0);
					    			    		if(!checkPassword==true){
					    			    			Toast.makeText(RegisterActivity.this, "Your passwords does not match", Toast.LENGTH_SHORT).show();
					    			    		}
								    		 	checkPassword=true; 
								    		 	errors=true;
					    			    	}else{
					    			    		if(password.length()>=6){
						    			    		password.requestFocus();
						    			    		repeat.requestFocus();
						    			    		password.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_valid, 0);
						    			    		repeat.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_valid, 0);
					    			    		}else{
					    			    			password.requestFocus();
						    			    		repeat.requestFocus();
						    			    		password.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_error, 0);
						    			    		repeat.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_error, 0);
						    			    		Toast.makeText(RegisterActivity.this, "Your password is too low", Toast.LENGTH_SHORT).show();
					    			    			errors=true;
					    			    		}
					    			    	}
			    			    	}
			    			    	if(field!=password && field!=repeat){
			    			    		
			    			    		if(field==email){
				    			    		if(Utils.isEmailValid(field.getText().toString())==false){
				    			    			field.requestFocus();
				    			    			field.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_error, 0);
				    			    			Toast.makeText(RegisterActivity.this, "Please enter a valid email", Toast.LENGTH_SHORT).show(); 
				    			    			errors=true;
				    			    		}else{
				    			    			field.requestFocus();
				    			    			field.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_valid, 0);
				    			    		}
										}else{
				    			    		field.requestFocus();
				    			    		field.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_valid, 0);
										}

			    			    	}
			    			    }
			    			}
			    		 
			    		 
			    		//TRAITEMENT DE L'INSCRIPTION
			    		 
			    		 if(errors==false){
			    			 
			    			 String sexe;
			    			 TextView textView = (TextView)sex.getSelectedView();
			    			 sexe = textView.getText().toString();
			    			 
			    			 String Mysexe;
			    			 if(sexe.toString().equals("Female")){
			    				 Mysexe ="F";
			    			 }else{
			    				 Mysexe ="H";
			    			 }
			    			 
			    			UserFunctions userFunction = new UserFunctions();
			                JSONObject json = userFunction.registerUser(firstname.getText().toString(),lastname.getText().toString(), email.getText().toString(), password.getText().toString(),age.getText().toString(),Mysexe.toString());
			                 
			                 try {
			                     if (json.getString(KEY_SUCCESS) != null) {
			                         String res = json.getString(KEY_SUCCESS); 
			                         if(Integer.parseInt(res) == 1){
			                             DatabaseHandler db = new DatabaseHandler(getApplicationContext());
			                             JSONObject json_user = json.getJSONObject("user");
			                             userFunction.logoutUser(getApplicationContext());
			                             Toast.makeText(RegisterActivity.this, "Thank you for your registration", Toast.LENGTH_SHORT).show();
			                             Intent i = new Intent(RegisterActivity.this, LoginActivity.class);
			                             i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
			                             startActivity(i);
			                             finish();
			                         }else{
			                        	 Toast.makeText(RegisterActivity.this, "ERROR", Toast.LENGTH_SHORT).show();
			                         }
			                     }
			                 } catch (JSONException e) {
			                     e.printStackTrace();
			                 }
			             
			    		 }
			    }
		});
		 
		/*returnLogin.setOnClickListener(new View.OnClickListener() {
		    public void onClick(View v) {
		        Intent i=new Intent(getBaseContext(),LoginActivity.class);
                startActivity(i);
                finish();
		    }
		});*/
	}

	
	
    public void onBackPressed() {
    	Intent i=new Intent(getBaseContext(),LoginActivity.class);
        startActivity(i);
        finish();
    }

}
